<!--
 * @Author: your name
 * @Date: 2021-01-07 14:02:36
 * @LastEditTime: 2021-01-12 11:00:29
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\test\deviceTest\deviceTest.vue
-->
<template>
    <div class="test-deviceTest-main-box">
        <div class="search-box">
            <div class="search-box-sub">
                <span class="lable-text">{{$t('salePage.wifiSN')}}</span>
                <el-input v-big v-model="wifiSN" clearable></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('test.deviceTest.real_time.iccid')}}</span>
                <el-input v-model="iccid" clearable></el-input>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('test.deviceTest.handleState')}}</span>
                <el-select v-model="status" filterable :placeholder="$t('pleaseSelect')" :clearable='true'>
                    <el-option
                    v-for="item in dictCodes.TEST_FINISH_STATUS"
                    :key="item.value"
                    :label="item.code"
                    :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('card.cardType')}}</span>
                <el-select v-model="cardType" filterable :placeholder="$t('pleaseSelect')" :clearable='true'>
                    <el-option
                    v-for="item in dictCodes.CARD_TYPE"
                    :key="item.value"
                    :label="item.code"
                    :value="item.value">
                    </el-option>
                </el-select>
            </div>
            <div class="search-box-sub">
                <span class="lable-text">{{$t('createTime')}}</span>
                <el-date-picker
                v-model="createTimeBefore"
                type="date"
                :placeholder="$t('selectDate')"
                value-format='yyyy-MM-dd'
                :clearable='true'
                :picker-options="pickerOptions"
                >
                </el-date-picker>
                -
                <el-date-picker
                v-model="createTimeAfter"
                type="date"
                :placeholder="$t('selectDate')"
                value-format='yyyy-MM-dd'
                :clearable='true'
                :picker-options="pickerOptions"
                >
                </el-date-picker>
            </div>
            <div class="search-box-sub">
                <el-button type="primary" @click='query' >{{$t('query')}}</el-button>
                <el-button type="primary" @click='exportModal' >{{$t('export')}}</el-button>
            </div>
        </div>
        <baseTable v-loading="loading"  row-key='sid' :data="dataRows" border  stripe @selection-change="handleSelectionChange($event)"  :column='column' :select='true'>
            <template #cardType="{data}">
              {{comm.returnCode(dictCodes.CARD_TYPE,data.cardType,'value','code')}}
            </template>
            <template #status="{data}">
              {{comm.returnCode(dictCodes.TEST_FINISH_STATUS,data.status,'value','code')}}
            </template>
            <template #option="{data}">
                <div class="option-box">
                    <el-popconfirm
                        :title="$t('test.test_finish_cardStatus_change')"
                        :confirm-button-text="$t('sure')"
                        :cancel-button-text="$t('cancel')"
                        @confirm='gprsChangeCardStatus(data)'
                    >
                        <el-button size='small' type="primary" icon="iconfont icon-swap" slot="reference"  :title='$t("devWifi.unbundling")'></el-button>
                    </el-popconfirm>
                </div>
            </template>
        </baseTable>
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
    </div>
</template>

<script>
import baseTable from '../../../components/common/baseTable'
export default {
    components: {
        baseTable,
    },
    props: {

    },
    data() {
        return {
            pickerOptions:{ //禁用今天之后的时间
                disabledDate(time) {
                    return time.getTime() > Date.now()
                }
            },
            dictCodes:{},
            wifiSN:'',
            iccid:'',
            createTimeBefore:'',
            createTimeAfter:'',
            status:'',
            cardType:'',
            loading:false,
            dataRows:[],
            column:[{
                title: this.$t('servicechange.wifiSN'),
                value: 'wifiSN',
                slot: 'wifiSN',
            },{
                title: this.$t('user.loginName'),
                value: 'loginName',
            },
            {
                title: this.$t('vsite.firmName'),
                value: 'firmName',
                slot: 'firmName',
            },{
                title: this.$t('test.deviceTest.real_time.iccid'),
                value: 'iccid',
                slot: 'iccid',
            },
            {
                title: this.$t('card.cardType'),
                value: 'cardType',
                slot: 'cardType',
            },{
                title: this.$t('sn.snStatus'),
                value: 'status',
                slot: 'status',
            },{
                title: this.$t('opttask.createTime'),
                value: 'createTime',
                slot: 'createTime',
            },{
                title: this.$t('opttask.option'),
                value: 'sid',
                slot: 'option',
            }],
            selectDataList:[],
            size:10,
            current:1,
            total:0
        };
    },
    computed: {

    },
    watch: {

    },
    created() {

    },
    mounted() {
        this.getDictionaryBatch()
        this.deviceTestGetPage()
    },
    methods: {
        exportModal(){
            this.comm.windowOpen('/deviceTest/export',{
                wifiSN:this.wifiSN,
                iccid:this.iccid,
                status:this.status,
                cardType:this.cardType,
                createTimeBefore:this.createTimeBefore,
                createTimeAfter:this.createTimeAfter,
            })
        },
        async gprsChangeCardStatus(data){
            let res = await this.API.gprsChangeCardStatus({
                wifiSn:data.wifiSN,
                id:data.sid,
            })
            if(this.comm.showMsg(res.success)){
                this.deviceTestGetPage()
            }
        },
        async recordExcel(row){
            this.comm.windowOpen('/deviceTest/exportExcel',{
                wifiSN:row.wifiSN,
                inverterSN:row.inverterSN,
                beginTime:row.beginTime,
                endTime:row.endTime,
            })
        },
        async deviceTestFinishBatch(){
            if(this.selectDataList.length == 0){
                this.comm.showMsg(false,this.$t('test.deviceTest.js_Please_choose_to_complete_the_aging_equipment'))
            }
            let res = await this.API.deviceTestFinish({
                wifiSns:this.selectDataList.map(item =>{
                    return item.wifiSN
                })
            })
            if(this.comm.showMsg(res.success)){
                this.deviceTestGetPage()
            }
        },
        async deviceTestDelete(wifiSN){
            let res = await this.API.deviceTestDelete({
                wifiSN:wifiSN
            })
            if(this.comm.showMsg(res.success)){
                this.deviceTestGetPage()
            }
        },
        alarmFormatter(row){
            var inverterAlarm=row.inverterAlarmNum==null?0:row.inverterAlarmNum;
            var batteryAlarm=row.batteryAlarmNum==null?0:row.batteryAlarmNum;
            return inverterAlarm+"+"+batteryAlarm;
        },
        continueFormatter(row){
            var beginTime = Date.parse(new Date(row.beginTime.replace(/-/g, "/"))); 
            var endTime=Date.parse(new Date(row.endTime.replace(/-/g, "/"))); 
            var date3=endTime-beginTime;
            
            var days=Math.floor(date3/(24*3600*1000));
            
        //	//计算出小时数

            var leave1=date3%(24*3600*1000)    //计算天数后剩余的毫秒数
            var hours=Math.floor(leave1/(3600*1000))
            //计算相差分钟数
            var leave2=leave1%(3600*1000)        //计算小时数后剩余的毫秒数
            var minutes=Math.floor(leave2/(60*1000))
            //计算相差秒数
            var leave3=leave2%(60*1000)      //计算分钟数后剩余的毫秒数
            var seconds=Math.round(leave3/1000)
            var totalHours=days*24+hours;
            return totalHours+' '+this.$t('test.deviceTest.testConfig.hour')+' '+minutes+' '+this.$t('test.deviceTest.js_deleteTestDevice_minutes')+' '+seconds+' '+this.$t('test.deviceTest.js_deleteTestDevice_second');
        },
        async deviceTestGetPage(){
            this.loading = true
            let res = await this.API.deviceTestGetTestFinishPage({
                iccid:this.iccid,
                wifiSN:this.wifiSN,
                cardType:this.cardType,
                status:this.status,
                createTimeBefore:this.createTimeBefore,
                createTimeAfter:this.createTimeAfter,
                size:this.size,
                current:this.current,
            })
            this.dataRows = res.rows
            this.total = res.total
            this.loading = false
        },
        addMoadl(){
            this.$store.commit('setNewVisible',true)
        },
        saleDeleteBatch(){

        },
        query(){
            this.current = 1
            this.deviceTestGetPage()
        },
        async getDictionaryBatch(){
            let res = await this.API.getDictionaryBatch({
                dictCodes:`TEST_FINISH_STATUS,CARD_TYPE`,
                lang:localStorage.getItem('lang') || 'zh_CN'
            })
            this.dictCodes = res
        },
        handleSelectionChange(data){
            this.selectDataList = data
        },
        handleSizeChange(val) {
          this.size = val
          this.current = 1
          this.deviceTestGetPage()
        },
        handleCurrentChange(val) {
          this.current = val
          this.deviceTestGetPage()
        },
    },
};
</script>

<style  lang="less" scoped>

</style>
